//
//  SCBrandTableHeaderView.m
//  Shuaiche
//
//  Created by dangshuai on 2018/12/26.
//  Copyright © 2018 zzs. All rights reserved.
//

#import "SCBrandTableHeaderView.h"
#import "UIView+Line.h"
#import "BrandUsed+CoreDataClass.h"
#import <MagicalRecord/MagicalRecord.h>

#import "UIImageView+SCwebImage.h"
@implementation SCBrandTableHeaderView {
    UIView *_contentView;
    UILabel *_titleLabel;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createSubview];
    }
    return self;
}

- (void)itemDidTap:(UITapGestureRecognizer *)gesture {
    SCBrandTableHeaderItem *view = (SCBrandTableHeaderItem *)gesture.view;
    if (_hasSelected) {
        return;
    }
    _hasSelected = YES;
    [_delegate brandTableHeaderViewDidSelectedName:view.nameLabel.text brandId:view.bid];
}
    
- (void)createSubview {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 50)];
    titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    titleLabel.textColor = SCColor(0x222222);
    titleLabel.text = @"常用品牌";
    [self addSubview:titleLabel];
    _titleLabel = titleLabel;
    
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, kSCScreenWidth, 90)];
    [_contentView addLineWithFrame:CGRectMake(10, 0, kSCScreenWidth, 0.5)];
    [self addSubview:_contentView];
    
    self.frame = CGRectMake(0, 0, kSCScreenWidth, 150);
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLabel.text = title;
}
    
- (void)updateData {
    CGFloat space = 10;
    CGFloat w = floor((kSCScreenWidth - space * 6) / 5.f);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *array = [self featchAll];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSInteger count = array.count;
            for (int i = 0; i < count; i++) {
                BrandUsed *br = array[count-i-1];
                SCBrandTableHeaderItem *item = [[SCBrandTableHeaderItem alloc] init];
                [_contentView addSubview:item];
                item.nameLabel.text = br.name;
                item.bid = br.id;
                [item.logoImageView sc_setImageWithPath:br.logo];
                item.frame = CGRectMake((w+space)*i + space, 0, w, 90);
                
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemDidTap:)];
                [item addGestureRecognizer:tap];
            }
        });
    });
}
    
- (NSArray *)featchAll {
    NSArray *array = [BrandUsed MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"type==%d",_usedType]];
    return array;
}

@end



@implementation SCBrandTableHeaderItem

- (instancetype)init {
    self = [super init];
    if (self) {
        [self createSubview];
    }
    return self;
}
    
- (void)layoutSubviews {
    [super layoutSubviews];
    _logoImageView.center = CGPointMake(CGRectGetMidX(self.bounds), 38);
    _nameLabel.frame = CGRectMake(0, CGRectGetMaxY(self.bounds) - 18, CGRectGetWidth(self.bounds), 13);
}

- (void)createSubview {
    _logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 40)];
    _logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_logoImageView];
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 54, 13)];
    _nameLabel.font = [UIFont systemFontOfSize:12];
    _nameLabel.textAlignment = 1;
    [self addSubview:_nameLabel];
}

@end
